package com.hy.utils;

import com.hy.bean.Dispose;
import com.hy.dao.DisposeDao;
import com.hy.dao.ReportDao;
import com.hy.dao.UserDao;
import com.hy.vo.DisposeReporeVo;
import lombok.Data;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @Description: 自定义定时任务
 * @Author: zhangzhengwei2006
 * @Date: 2024-01-17 13:58
 *
 *
 */
public class MyRunnableUtils implements Runnable {

    private final long timeInterval = 1000 * 10;
    public static volatile boolean isInterrupt = false;
    DisposeDao disposeDao = new DisposeDao();
    UserDao userDao = new UserDao();
    ReportDao reportDao = new ReportDao();
    @Override
    public void run() {
        while (!isInterrupt){
            Date date = new Date(); // 当前时间
            try {
                Connection conn = DBManager.getConn();
                conn.setAutoCommit(false);
                List<DisposeReporeVo> disposes = disposeDao.selectList();// 查询所有被处理的用户
                if(disposes.size() != 0){
                    for (DisposeReporeVo e : disposes) {
                        boolean after = date.after(e.getDisposeDate());// 判断当前时间是否在DisposeDate之前
                        System.out.println(after);
                        if (after){
                            if (userDao.updateUserStatusByuid(e.getUid(),conn) > 0) {// 修改成功
                                if (disposeDao.deleteBydid(e.getUid(),conn) > 0) { // 修改成功
                                    if (reportDao.deaeteByid(e.getRid(),conn) > 0){
                                        conn.commit();// 手动提交 全部成功就提交
                                        DbUtils.close(conn);
                                    }
                                }else {
                                    conn.rollback();// 否则回滚
                                    DbUtils.close(conn);
                                }
                            }
                        }
                    }
                }else {
                    isInterrupt = true;
                    System.out.println("停止线程");
                }
                Thread.sleep(timeInterval);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
